/*
 * @Author: fbf
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \component-injection-framework-connector\src\CIF\API\api.js
 */
// import request from '@/utils/request';
import axios from 'axios';
import Qs from 'qs';
// const defaultTenantId = '200158';

const MYSVIEW_SERVICE = '/res-center';

const MAP_VIEW = '/map-view';

export function getPackageFile(filePath = '', callback) {
	return axios({
		baseURL: './', // url = base url + request url
		method: 'get',
		url: `${filePath}`,
		responseType: 'blob',
		transformResponse: [
			function (data) {
				// Do whatever you want to transform the data
				if (data.size === 0) {
					callback && callback();
				}
				return data;
			}
		],
		onDownloadProgress: (progressEvent) => {
			// let percentCompleted = Math.floor((progressEvent.loaded * 100) / progressEvent.total);
			// do whatever you like with the percentage complete
			// maybe dispatch an action that will update a progress bar or something
			if (progressEvent.total === 0) {
				callback && callback();
			} else {
				if (progressEvent.loaded == progressEvent.total) {
					// console.log("fireComponentFetchingProgressNotification", progressEvent.loaded, progressEvent.total)
					callback && callback();
				}
			}
		}
	});
}

// export function getPackageFile(fileToken = '', callback) {
//   return request({
//     method: 'get',
//     url: '/sys-storage/download',
//     responseType: 'blob',
//     params: {
//       f8s: fileToken,
//     },
//     transformResponse: [function (data) {
//       // Do whatever you want to transform the data
//       if (data.size === 0) {
//         callback && callback()
//       }
//       return data;
//     }],
//     onDownloadProgress: (progressEvent) => {
//       // let percentCompleted = Math.floor((progressEvent.loaded * 100) / progressEvent.total);
//       // do whatever you like with the percentage complete
//       // maybe dispatch an action that will update a progress bar or something
//       if (progressEvent.total === 0) {
//         callback && callback()
//       } else {
//         if (progressEvent.loaded == progressEvent.total) {
//           // console.log("fireComponentFetchingProgressNotification", progressEvent.loaded, progressEvent.total)
//           callback && callback()
//         }
//       }
//     },
//   })
// }

export function getThemeConfiguration() {
	const data = [
		{
			id: 'theme1_id',
			name: 'theme1',
			discription: 'disc',
			config: {
				colorA: '#fff',
				colorB: '#fff'
			}
		}
	];
	return new Promise((resolve) => {
		resolve(data);
	});
}

export function getComponentLibraryAll() {
	return axios({
		method: 'get',
		url: `${MYSVIEW_SERVICE}${MAP_VIEW}/component/all`,
		params: {
			// classification: '',
			// componentName: ''
			// tenantId: getQueryVariable('tenantId') || defaultTenantId
		}
	});
}

export function getComponentLibraryByIds(ids = []) {
	return axios({
		url: `${MYSVIEW_SERVICE}${MAP_VIEW}/component/ids`,
		method: 'get',
		paramsSerializer: function (params) {
			return Qs.stringify(params, { arrayFormat: 'repeat' });
		},
		params: {
			ids: ids
			// tenantId: getQueryVariable('tenantId') || defaultTenantId
		}
	});
}

export function getApplicationConfiguration(appId = '') {
	return axios({
		url: `${MYSVIEW_SERVICE}${MAP_VIEW}/application`,
		method: 'get',
		params: {
			id: appId
			// tenantId: getQueryVariable('tenantId') || defaultTenantId
		}
	});
}

export function updateApplicationConfiguration(data) {
	return axios({
		url: `${MYSVIEW_SERVICE}${MAP_VIEW}/application`,
		method: 'put',
		data: {
			...data
			// tenantId: getQueryVariable('tenantId') || defaultTenantId
		}
	});
}

/**
 * 根据组件定义id获取组件绑定的示例数据
 * @param {String} cpntDefId 组件定义id
 * @returns Promise
 */
export function getComponentExampleData(cpntDefId) {
	return axios({
		url: `${MYSVIEW_SERVICE}${MAP_VIEW}/component/example`,
		method: 'get',
		params: {
			componentId: cpntDefId
			// tenantId: getQueryVariable('tenantId') || defaultTenantId
		}
	});
}

// 获取图标文件列表
export function getIconList() {
	return axios({
		url: `${MYSVIEW_SERVICE}${MAP_VIEW}/assets/icon/list`,
		method: 'get'
		// params: { tenantId: getQueryVariable('tenantId') || defaultTenantId }
	});
}

// 上传图标文件
export function uploadIcon(formData) {
	return axios({
		url: `${MYSVIEW_SERVICE}${MAP_VIEW}/assets/icon`,
		method: 'POST',
		data: formData
	});
}

// 获取图片边框文件列表
export function getImageBorderList(params) {
	return axios({
		url: `${MYSVIEW_SERVICE}${MAP_VIEW}/assets/background/list`,
		method: 'get',
		params: {
			...params
			//  tenantId: getQueryVariable('tenantId') || defaultTenantId
		}
	});
}

// 上传图片边框文件
export function uploadImageBorder(formData) {
	return axios({
		url: `${MYSVIEW_SERVICE}${MAP_VIEW}/assets/background`,
		method: 'POST',
		data: formData
	});
}

// 获取组件静态资源列表
export function getStaticResource(componentName = '', version = '', type = '') {
	return axios({
		url: `${MYSVIEW_SERVICE}${MAP_VIEW}/assets/static-resource`,
		method: 'get',
		params: type
			? {
					componentName,
					version,
					type
					// tenantId: getQueryVariable('tenantId') || defaultTenantId
				}
			: {
					componentName,
					version
					// tenantId: getQueryVariable('tenantId') || defaultTenantId
				}
	});
}
// function getQueryVariable(variable) {
// 	const query = window.location.search.substring(1);
// 	const vars = query.split('&');
// 	for (let i = 0; i < vars.length; i++) {
// 		const pair = vars[i].split('=');
// 		if (pair[0] == variable) {
// 			return pair[1];
// 		}
// 	}
// 	return 'test';
// }
// 上传组件静态资源文件
export function uploadStaticResource(componentName = '', version = '', type = '', file) {
	const formData = new FormData();
	formData.append('file', file);
	formData.append('componentName', componentName);
	formData.append('version', version);
	formData.append('type', type);
	// formData.append('tenantId', getQueryVariable('tenantId'));

	return axios({
		url: `${MYSVIEW_SERVICE}${MAP_VIEW}/assets/static-resource`,
		method: 'POST',
		data: formData
	});
}

/**
 * 批量获取服务信息
 * @param {String[]} ids ServiceInfo对象id数组
 * @returns
 */
export function getServiceInfoByIds(ids = []) {
	return axios({
		url: `${MYSVIEW_SERVICE}${MAP_VIEW}/service/ids`,
		method: 'get',
		params: {
			ids: ids.join(',')
		}
	});
}
